package io.coderyeah.ymcc.web.controller;

import io.coderyeah.ymcc.service.IKillActivityService;
import io.coderyeah.ymcc.domain.KillActivity;
import io.coderyeah.ymcc.query.KillActivityQuery;
import io.coderyeah.ymcc.result.JSONResult;
import io.coderyeah.ymcc.result.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;

@RestController
@RequestMapping("/killActivity")
public class KillActivityController {


    @Autowired
    public IKillActivityService killActivityService;

    /**
     * 保存和修改公用的
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody KillActivity killActivity) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd");

        if (killActivity.getId() != null) {
            killActivityService.updateById(killActivity);
        } else {
            // 设置状态
            killActivity.setPublishStatus(KillActivity.KILL_WAIT_PUBLISH);
            // 设置时间str
            killActivity.setTimeStr(dateFormat.format(killActivity.getStartTime()));
            // 保存活动
            killActivityService.insert(killActivity);
        }
        return JSONResult.success();
    }

    /**
     * 删除对象
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id) {
        killActivityService.deleteById(id);
        return JSONResult.success();
    }

    /**
     * 获取对象
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id") Long id) {
        return JSONResult.success(killActivityService.selectById(id));
    }


    /**
     * 查询所有对象
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public JSONResult list() {
        return JSONResult.success(killActivityService.selectList(null));
    }


    /**
     * 带条件分页查询数据
     */
    @RequestMapping(value = "/pagelist", method = RequestMethod.POST)
    public JSONResult page(@RequestBody KillActivityQuery query) {
        Page<KillActivity> page = new Page<KillActivity>(query.getPage(), query.getRows());
        page = killActivityService.selectPage(page);
        return JSONResult.success(new PageList<KillActivity>(page.getTotal(), page.getRecords()));
    }

    @PostMapping("/publish/{id}")
    public JSONResult publish(@PathVariable Long id) {
        killActivityService.publish(id);
        return JSONResult.success();
    }
}
